﻿
app.controller('EventReportController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;

    $scope.AddEventReport = function (model) {
        if (model.PlaceId == null || model.PlaceId == "0") {
            alertify.alert("请选择场所");
            return false;
        }
        if (model.EventContent == null || model.EventContent == "") {
            alertify.alert("填写事件内容");
            return false;
        }
        var flag = $("#level2").prop("checked");
        if (flag) {
            model.ReportLevel = 2;
        }
        else {
            model.ReportLevel = 1;
        }
        var files = document.querySelector('input[type=file]').files;
        Upload.upload({
            method: 'POST',
            url: '../EventReport/AddEventReport',
            data: { 'model': model },
            file: files
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("事件添加成功！");

                    setTimeout(function () {
                        location.href = "EventReportPageList";
                    }, 2000)
                } else
                    alertify.alert(ret.Message);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }
    $scope.MAddEventReport = function (model) {
        
        if (model.PlaceId == null || model.PlaceId == "") {
            mui.toast("请选择场所");
            return false;
        }
        if (model.EventContent == null || model.EventContent == "") {
            mui.toast("填写事件内容");
            return false;
        }
        $scope.Loading = true;
        model.ReportTime = $("#datetime").val();
        model.CheckTime = $("#datetime2").val();
        var flag = $("#level2").prop("checked");
        if (flag) {
            model.ReportLevel = 2;
        }
        else {
            model.ReportLevel = 1;
        }
        $http({
            method: 'POST',
            url: '../EventReport/AddEventReport',
            data: { 'model': model, "FileList": $scope.FileList }
        })
            .success(function (ret) {
                $scope.Loading = false;
                if (ret.Code == 100) {
                    mui.toast("事件添加成功！");

                    setTimeout(function () {
                        location.href = "MEventReportPageList";
                    }, 1000)
                } else
                    mui.toast(ret.Message);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }
    $scope.MEditEventReport = function (model) {
        if (model.PlaceId == null || model.PlaceId == "") {
            mui.toast("请选择场所");
            return false;
        }
        if (model.EventContent == null || model.EventContent == "") {
            mui.toast("填写事件内容");
            return false;
        }
        $scope.Loading = true;
        model.ReportTime = $("#datetime").val();
        model.CheckTime = $("#datetime2").val();
        var flag = $("#level2").prop("checked");
        if (flag) {
            model.ReportLevel = 2;
        }
        else {
            model.ReportLevel = 1;
        }
        $http({
            method: 'POST',
            url: '../EventReport/EditEventReport',
            data: { 'model': model, "FileList": $scope.FileList }
        })
            .success(function (ret) {
                $scope.Loading = false;
                if (ret.Code == 100) {
                    mui.toast("事件修改成功！");

                    setTimeout(function () {
                        location.href = "MEventReportPageList";
                    }, 1000)
                } else
                    mui.toast(ret.Message);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }
    $scope.GetParkList = function () {
        $scope.ParkList = [];
        $http({
            method: 'POST',
            url: '../SafetyConfig/GetParkList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.ParkList = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }
    $scope.MGetPlaceList = function (id) {
        $scope.PlaceList = [];
        $http({
            method: 'POST',
            url: '../IssueReport/GetParkPlace',
            data: { "ParkId": id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.PlaceList = ret.Data;
                }
                else {
                    mui.toast(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                mui.toast("请检查网络");
            });
    }
    $scope.GetPlaceList = function (control, id, defaultvalue) {
        if (id == null || id == "")
            return false;
        $("#" + control).html($compile('<option value="" selected>请选择场所</option>')($scope));
        $("#" + control).selectpicker('refresh');
        $http({
            method: 'POST',
            url: '../IssueReport/GetParkPlace',
            data: { "ParkId": id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $.each(ret.Data, function (i, n) {
                        if (defaultvalue != null) {
                            if (n.Id == defaultvalue)
                                $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + n.PlaceName + "</option>")($scope));
                            else
                                $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.PlaceName + "</option>")($scope));
                        }
                        else {
                            $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.PlaceName + "</option>")($scope));
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetEventReportPageList = function (model, page) {
        $scope.EventReportList = [];
        $scope.DataLoading = true;
        $http({
            method: 'POST',
            url: '../EventReport/GetEventReportPageList',
            data: { 'model': model, 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.EventReportList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                $scope.DataLoading = false;
                alertify.alert("请检查网络");
            });
    }
    $scope.BindDepartmentList = function (control, defaultvalue) {
        $http({
            method: 'POST',
            url: '../HiddenTrouble/GetDepartmentList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $.each(ret.Data, function (i, n) {
                        if (n.DepartmentName != "系统支持") {
                            if (defaultvalue != null) {
                                if (n.Id == defaultvalue)
                                    $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                                else
                                    $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                            }
                            else {
                                $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                            }
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.DeleteEventReport = function (id, searchmodel, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $scope.PageLoading = true;
                $http({
                    method: 'POST',
                    url: '../EventReport/DeleteEventReport',
                    data: { 'Id': id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            $scope.AlertBox("删除成功！");
                            $scope.GetEventReportPageList(searchmodel, page);
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.MDeleteEventReport = function (id) {
        mui.confirm('确认删除该事件报告？', '系统提醒', ['否', '是'], function (e) {
            if (e.index == 1) {
                $http({
                    method: 'POST',
                    url: '../EventReport/DeleteEventReport',
                    data: { 'Id': id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        $scope.PageLoading = false;
                        if (ret.Code == 100) {
                            mui.toast(ret.Msg);
                            $("#" + id).remove();
                        }
                        else {
                            mui.toast(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alertify.alert("请检查网络");
                    });


            } else {
                //mui.toast("");
            }
        })
    }
    $scope.MGetPlaceList = function (id) {
        $scope.PlaceList = [];
        $http({
            method: 'POST',
            url: '../IssueReport/GetParkPlace',
            data: { "ParkId": id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.PlaceList = ret.Data;
                }
                else {
                    mui.toast(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                mui.toast("请检查网络");
            });
    }

    $scope.MGetEventReportDetail = function (Id) {
        $scope.EventReportDetail = {};
        $http({
            method: 'POST',
            url: '../EventReport/GetEventReportDetail',
            data: { 'Id': Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.EventReportDetail = ret.Data;
                    $scope.EventReportDetail.ParkId = $scope.EventReportDetail.ParkId + "";
                    $scope.EventReportDetail.PlaceId = $scope.EventReportDetail.PlaceId + "";
                    $scope.MGetPlaceList($scope.EventReportDetail.ParkId);
                    $scope.EventReportDetail.ReportTime = $filter('JSONDate')($scope.EventReportDetail.ReportTime, 'yyyy-MM-dd HH:mm');
                    $scope.EventReportDetail.CreateTime = $filter('JSONDate')($scope.EventReportDetail.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                    $scope.EventReportDetail.CheckTime = $filter('JSONDate')($scope.EventReportDetail.CheckTime, 'yyyy-MM-dd HH:mm'); 
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetEventReportDetail = function (Id) {
        $scope.EventReportDetail = {};
        $http({
            method: 'POST',
            url: '../EventReport/GetEventReportDetail',
            data: { 'Id': Id }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.EventReportDetail = ret.Data;
                    $scope.EventReportDetail.ParkId = $scope.EventReportDetail.ParkId + "";
                    $scope.GetPlaceList('placelist', $scope.EventReportDetail.ParkId, $scope.EventReportDetail.PlaceId);
                    $scope.EventReportDetail.ReportTime = $filter('JSONDate')($scope.EventReportDetail.ReportTime, 'yyyy-MM-dd HH:mm');
                    $scope.EventReportDetail.CheckTime = $filter('JSONDate')($scope.EventReportDetail.CheckTime, 'yyyy-MM-dd HH:mm');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.DeleteEventReportFile = function (Id, filelist) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认此操作？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../EventReport/DeleteEventReportFile',
                    data: { 'Id': Id }
                })
                    .success(function (ret) {
                        common.success($scope, null, ret);
                        if (ret.Code == 100) {
                            for (var i = 0; i < filelist.length; i++) {
                                if (filelist[i].FileId == Id) {
                                    filelist.splice(i, 1);
                                }
                            }
                            $scope.AlertBox("删除成功！");
                        }
                        else {
                            alertify.alert(ret.Msg);
                        }
                    })
                    .error(function (data, status, headers, config) {
                        $scope.PageLoading = false;
                    });
            }, function (ev) {
                ev.preventDefault();
            });
    }

    $scope.EditEventReport = function (model, page) {
        if (model.PlaceId == null || model.PlaceId == "0") {
            alertify.alert("请选择场所");
            return false;
        }
        if (model.EventContent == null || model.EventContent == "") {
            alertify.alert("填写事件内容");
            return false;
        }
        var files = document.querySelector('input[type=file]').files;
        var flag = $("level2").prop("checked");
        if (flag) {
            model.ReportLevel = 2;
        }
        else {
            model.ReportLevel = 1;
        }
        Upload.upload({
            method: 'POST',
            url: '../EventReport/EditEventReport',
            data: { 'model': model },
            file: files
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("事件报告修改成功！");
                    setTimeout(function () {
                        location.href = "EventReportPageList?page=" + page;
                    }, 2000)
                } else
                    alertify.alert(ret.Message);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }

    $scope.MGetEventReportDealList = function (model, page) {
        if (page == 1) {
            $scope.EventReportList = [];
        }
        $scope.PageIndex = page;
        $http({
            method: 'POST',
            url: '../EventReport/GetEventReportPageList',
            data: { 'model': model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    for (var i = 0; i < ret.Data.Data.length; i++) {
                        $scope.EventReportList.push(ret.Data.Data[i]);
                    }
                    if (ret.Data.Data.length == 0) {
                        mui('#pullrefresh').pullRefresh().endPullupToRefresh(true);
                    }
                    else {
                        mui('#pullrefresh').pullRefresh().endPullupToRefresh(false);
                    }
                }
                else {
                    mui.toast(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.InitEventReport = function () {
        $scope.PageIndex = 1;
        mui.init({
            pullRefresh: {
                container: '#pullrefresh',
                down: {
                    style: 'circle',
                    auto: true,//可选,默认false.首次加载自动上拉刷新一次
                    callback: function () {
                        setTimeout(function () {
                            $scope.MGetEventReportDealList($scope.SearchModel, 1);
                            mui('#pullrefresh').pullRefresh().refresh(true);
                            mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
                        }, 1500);

                    }
                }, up: {
                    auto: false,
                    contentrefresh: '正在加载...',
                    callback: function () {
                        $scope.MGetEventReportDealList($scope.SearchModel, $scope.PageIndex + 1);
                    },

                }
            }
        });
    }

    $scope.InitImgeUpload = function () {
        setTimeout(function () {
            for (var i = 0; i < $scope.ImageCount.length; i++) {
                var imagebox = "#imagebox_" + $scope.ImageCount[i];
                var dragImgUpload = new DragImgUpload(imagebox, {
                    callback: function (files) {
                        //回调函数，可以传递给后台等等
                        var file = files[0];
                        Upload.upload({
                            method: 'POST',
                            url: '../HiddenTrouble/UploadFormImage',
                            data: { "Category": 'EventReport' },
                            file: file
                        })
                            .success(function (ret) {
                                $scope.Submiting = false;
                                if (ret.Code == 100) {
                                    $scope.FileList.push(ret.Data);
                                    console.log($scope.FileList);

                                } else
                                    alertify.alert(ret.Msg);
                            })
                            .error(function (data, status, headers, config) {
                                common.error($scope);
                                mui.toast("请检查网络");
                            })
                    }
                }, "", $scope.ImageCount[i]);
            }
        }, 2000);
    }

    $scope.InitDatePicker = function (control) {
        (function ($) {
            $.init();
            var btns = $(control);
            btns.each(function (i, btn) {
                btn.addEventListener('tap', function () {
                    var _self = this;
                    if (_self.picker) {
                        _self.picker.show(function (rs) {
                            //jQuery(control).val();
                            jQuery(_self).val(rs.text);
                            _self.picker.dispose();
                            _self.picker = null;
                        });
                    } else {
                        var optionsJson = this.getAttribute('data-options') || '{}';
                        var options = JSON.parse(optionsJson);
                        var id = this.getAttribute('id');
                        _self.picker = new $.DtPicker(options);
                        _self.picker.show(function (rs) {
                            //jQuery(control).val(rs.text);
                            jQuery(_self).val(rs.text);
                            _self.picker.dispose();
                            _self.picker = null;
                        });
                    }
                }, false);
            });
        })(mui);
    }

});